package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.AccountingRecordMapper;
import com.guoning.office_system.model.*;
import com.guoning.office_system.service.AccountingRecordService;
import com.guoning.office_system.service.MeetingRoomService;
import com.guoning.office_system.service.MeetingService;
import com.guoning.office_system.service.UserService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AccountingRecordServiceImpl implements AccountingRecordService {
    @Autowired
    AccountingRecordMapper accountingRecordMapper;
    @Autowired
    UserService userService;
    @Autowired
    MeetingRoomService meetingRoomService;
    @Autowired
    MeetingService meetingService;

    @Override
    public Long selectByConditionCount(Criteria criteria) {
        return accountingRecordMapper.selectByConditionCount(criteria);
    }

    @Override
    public PageBean selectByCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageIndex()*pageBean.getPageSize());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        List<AccountingRecordWithUserAndRoom> list = accountingRecordMapper.selectByCondition(criteria);
        for(int i=0; i<list.size(); i++){
            User user = userService.selectByPrimaryKey(list.get(i).getUserId());
            MeetingRoom meetingRoom = meetingRoomService.selectByPrimaryKey(list.get(i).getRoomId());
            list.get(i).setUser(user);
            list.get(i).setMeetingRoom(meetingRoom);
        }
        Long count = accountingRecordMapper.selectByConditionCount(criteria);
        pageBean.setResultList(list);
        pageBean.setTotalCount(count);
        return pageBean;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(AccountingRecord record) {
        accountingRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public AccountingRecordWithUserAndRoom selectByPrimaryKey(Long id) {
        AccountingRecordWithUserAndRoom accountingRecordWithUserAndRoom = accountingRecordMapper.selectByPrimaryKey(id);
        MeetingRoom meetingRoom = meetingRoomService.selectByPrimaryKey(accountingRecordWithUserAndRoom.getRoomId());
        User user = userService.selectByPrimaryKey(accountingRecordWithUserAndRoom.getUserId());
        accountingRecordWithUserAndRoom.setMeetingRoom(meetingRoom);
        accountingRecordWithUserAndRoom.setUser(user);
        return accountingRecordWithUserAndRoom;
    }

    @Override
    public int updateByPrimaryKeySelective(AccountingRecord record) {
        record.setModifiedTime(new Date());
        return accountingRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateForDelete(List<AccountingRecord> recordList, User user) {
        List<Long> list = new ArrayList<>();
        for (int i=0; i<recordList.size(); i++){
            recordList.get(i).setModifiedTime(new Date());
            recordList.get(i).setModifier(user.getUserName());
            list.add(recordList.get(i).getId());
        }
        int i = accountingRecordMapper.updateForDelete(recordList);
        meetingService.deleteByRecordId(list);
        return i;
    }

    @Override
    public int updateRecordStatus(List<AccountingRecord> recordList, User user, Integer status) {
        for (int i=0; i<recordList.size(); i++){
            recordList.get(i).setModifier(user.getUserName());
            recordList.get(i).setModifiedTime(new Date());
            recordList.get(i).setRecordStatus(status);
        }
        int i = accountingRecordMapper.updateRecordStatus(recordList);
        return i;
    }

    @Override
    public List<AccountingRecordWithUserAndRoom> selectByTimeAndRecordId(AccountingRecord accountingRecord) {
        return accountingRecordMapper.selectByTimeAndRecordId(accountingRecord);
    }

}